package com.ubs.training.server.dao.impl.jdbc;

import com.ubs.training.server.dao.LinkTypeDao;
import com.ubs.training.server.dao.model.LinkType;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import javax.sql.DataSource;
import java.util.Collections;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xubuntu
 * Date: 8/21/12
 * Time: 5:22 AM
 * To change this template use File | Settings | File Templates.
 */
public class NamedParamLinkTypeDaoImpl implements LinkTypeDao {

    DataSource ds;

    NamedParameterJdbcOperations ops;

    public DataSource getDs() {
        return ds;
    }

    public void setDs(DataSource ds) {
        this.ds = ds;
        ops = new NamedParameterJdbcTemplate(ds);
    }


    @Override
    public List<LinkType> getAll() {
        return ops.query("select id, title from linked_type", Collections.EMPTY_MAP, new BeanPropertyRowMapper(LinkType.class));
    }

    @Override
    public void add(LinkType linkType) {
        int id = ops.queryForInt("select next value for cmstr_seq from dual", Collections.EMPTY_MAP);
        linkType.setId(id);
        ops.update("insert into linked_type values (:id, :title)", new BeanPropertySqlParameterSource(linkType));
    }


}
